package c2.cross.business.championrace.temp;

import cate.common.table.d.GDChampionRace;
import cate.game.framework.index.IndexedList;
import cate.game.pvp.championrace.data.ChampionRaceRedPacketRecord;
import cate.game.pvp.championrace.data.ChampionRaceSpec;
import cate.game.role.RoleSnapshot;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

public class ChampionRaceTempRedPacketData extends IndexedList<String, ChampionRaceRedPacketRecord> {
    private ChampionRaceRedPacketRecord query(ChampionRaceSpec spec, String uid) {
        if (Objects.isNull(spec) || !StringUtils.hasLength(uid)) {
            return null;
        }
        Map<String, ChampionRaceRedPacketRecord> indexMap = getIndex(spec.toString());
        if (CollectionUtils.isEmpty(indexMap)) {
            return null;
        }
        return indexMap.values()
                .stream()
                .filter(rd -> StringUtils.endsWithIgnoreCase(uid, rd.getUid()))
                .findFirst()
                .orElse(null);
    }

    private List<ChampionRaceRedPacketRecord> query(ChampionRaceSpec spec, long limit) {
        if (Objects.isNull(spec)) {
            return Collections.emptyList();
        }
        Map<String, ChampionRaceRedPacketRecord> indexMap = getIndex(spec.toString());
        if (CollectionUtils.isEmpty(indexMap)) {
            return Collections.emptyList();
        }
        return indexMap.values()
                .stream()
                .limit(limit)
                .collect(Collectors.toList());
    }

    private void insert(ChampionRaceSpec spec, ChampionRaceRedPacketRecord record) {
        addValue(record);
        getIndex(spec.toString()).put(record.getIndexedKey(), record);
    }

    public boolean hasRecvdRedPacket(ChampionRaceSpec spec, String roleUid) {
        return query(spec, roleUid) != null;
    }

    public List<ChampionRaceRedPacketRecord> getRedPacket(ChampionRaceSpec spec, RoleSnapshot snapshot) {
        ChampionRaceRedPacketRecord record = query(spec, snapshot.base.uid);
        if (Objects.isNull(record)) {
            record = ChampionRaceRedPacketRecord.of(spec, snapshot, GDChampionRace.FIXED_BET_NUM);
            insert(spec, record);
        }
        List<ChampionRaceRedPacketRecord> queryList = query(spec, 10);
        boolean b = queryList.stream().anyMatch(rd -> StringUtils.endsWithIgnoreCase(snapshot.base.uid, rd.getUid()));
        if (!b) {
            queryList.add(queryList.size() - 1, record);
        }
        return queryList;
    }

    public List<ChampionRaceRedPacketRecord> getRoleRedPacket(ChampionRaceSpec spec, String roleUid) {
        ChampionRaceRedPacketRecord record = query(spec, roleUid);
        List<ChampionRaceRedPacketRecord> queryList = query(spec, 10);
        if (Objects.isNull(queryList)) {
            return queryList;
        }
        boolean b = queryList.stream().anyMatch(rd -> StringUtils.endsWithIgnoreCase(roleUid, rd.getUid()));
        if (!b && Objects.nonNull(record)) {
            queryList.add(queryList.size() - 1, record);
        }
        return queryList;
    }
}
